from fastapi import Depends, Request
from fastapi_jwt_auth import AuthJWT
from fastapi.responses import JSONResponse

from new_auth.schemas.auth import auth
from common.base.base_routers import get_custom_router
from services.automatic_control.api import AutomaticControlApi
from services.server_integration.api import ServerIntegrationApi
from services.server_integration.schemas.alarms_request import AlarmsRequest
from services.server_integration.schemas.cities_timezone import TimezonesRequest
from services.new_services.new_server_integration.schemas.time_gps import GpsTime
from services.new_services.new_server_integration.api import NewServerIntegrationApi
from services.new_services.new_server_integration.schemas.block_info import BlockInfoRequest
from services.new_services.new_server_integration.schemas.alarms_request import NewAlarmsRequest
from services.new_services.new_server_integration.schemas.alarm_configuration import DeleteAlarms
from services.new_services.new_server_integration.schemas.response_message import ResponseMessage
from services.new_services.new_server_integration.schemas.short_block_info import ShortBlockInfoResponse
from services.new_services.new_server_integration.schemas.enable_configuration import LinksConnectionType
from services.new_services.new_server_integration.schemas.cviya_configuration import AvailableConnectionRequest
from services.new_services.new_server_integration.schemas.full_unit_configuration import NewFullUnitConfiguration
from services.new_services.new_server_integration.schemas.change_block_mode_configuration import ChangeBlockModeRequest


ac_api = AutomaticControlApi()
si_api = ServerIntegrationApi()
new_si_api = NewServerIntegrationApi()

new_server_integration_router = get_custom_router(
    new_si_api, prefix='/new_server_integration', tags=['new_server_integration'], base_routers=False
)


@new_server_integration_router.post('/delete-alarms', response_class=JSONResponse)
async def delete_alarms(alarms_to_be_deleted: DeleteAlarms, request: Request, authorize: AuthJWT = Depends()) -> ResponseMessage:
    is_user, user_role = await auth(authorize, request)
    return await new_si_api.delete_alarms(alarms_to_be_deleted)


@new_server_integration_router.post('/upload-configuration', response_class=JSONResponse)
async def upload_configuration(config: NewFullUnitConfiguration, request: Request, authorize: AuthJWT = Depends()) -> ResponseMessage:
    is_user, user_role = await auth(authorize, request)
    return await new_si_api.upload_configuration(config, user_role)


@new_server_integration_router.post('/get-cities-timezones', response_class=JSONResponse)
async def get_cities_timezones(timezone_request: TimezonesRequest, request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await new_si_api.get_cities_timezones(timezone_request)


@new_server_integration_router.post('/change-block-mode', response_class=JSONResponse)
async def changing_block_mode(config: ChangeBlockModeRequest, request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await new_si_api.change_block_mode(config)


@new_server_integration_router.post('/enable-radio-or-optics', response_class=JSONResponse)
async def enable_radio_or_optics(config: LinksConnectionType, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    return await new_si_api.enable_radio_or_optics(config, user_role)


@new_server_integration_router.post('/get-log-alarms', response_class=JSONResponse)
async def get_log_alarms(alarm_request: NewAlarmsRequest, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    return await new_si_api.get_log_alarms(alarm_request, user_role)


@new_server_integration_router.post('/get-current-alarms', response_class=JSONResponse)
async def get_current_alarms(alarm_request: NewAlarmsRequest, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    return await new_si_api.get_current_alarms(alarm_request, user_role)


@new_server_integration_router.post('/get-subblocks-statuses', response_class=JSONResponse)
async def get_subblocks_statuses(request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await new_si_api.get_subblocks_statuses()


@new_server_integration_router.post('/get-telemetry', response_class=JSONResponse)
async def get_telemetry(request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    return await new_si_api.get_telemetry(user_role)


@new_server_integration_router.post('/get-available-connections-by-cviya', response_class=JSONResponse)
async def get_available_connection(config: AvailableConnectionRequest, request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await new_si_api.get_available_connection(config)


@new_server_integration_router.post('/get-block-info', response_class=JSONResponse)
async def get_block_info(config: BlockInfoRequest, request: Request, authorize: AuthJWT = Depends()) -> ShortBlockInfoResponse:
    await auth(authorize, request)
    return await new_si_api.get_block_info(config)


@new_server_integration_router.get('/get-block-version', response_class=JSONResponse)
async def get_block_version(request: Request):
    return await new_si_api.get_block_version()


@new_server_integration_router.get('/get-total-log-alarms-count', response_class=JSONResponse)
async def get_total_log_alarms(request: Request, server_user_role: str = None, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)

    if user_role == 'server' and server_user_role == 'adjuster':
        user_role = server_user_role

    alarm_request: AlarmsRequest = AlarmsRequest()
    return await new_si_api.get_total_log_alarms(alarm_request, user_role)


@new_server_integration_router.get('/get-total-current-alarms-count', response_class=JSONResponse)
async def get_total_current_alarms(request: Request,  server_user_role: str = None, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)

    if user_role == 'server' and server_user_role == 'adjuster':
        user_role = server_user_role

    alarm_request: AlarmsRequest = AlarmsRequest()
    return await new_si_api.get_current_total_alarms(alarm_request, user_role)


@new_server_integration_router.post('/set-new-full-unit-configuration', response_class=JSONResponse)
async def set_new_full_unit_configuration(config: NewFullUnitConfiguration, request: Request, authorize: AuthJWT = Depends()) -> ResponseMessage:
    is_user, user_role = await auth(authorize, request)
    return await new_si_api.set_new_full_configuration(config, user_role)


@new_server_integration_router.post('/get-new-full-unit-configuration', response_class=JSONResponse)
async def get_new_full_unit_configuration(request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await new_si_api.get_new_full_configuration()


@new_server_integration_router.get('/get-gps-time', response_class=JSONResponse)
async def get_gps_time(request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await new_si_api.get_gps_time()


@new_server_integration_router.post('/set-gps-time', response_class=JSONResponse)
async def set_gps_time(gps_time: GpsTime, request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await new_si_api.set_gps_time(gps_time)


@new_server_integration_router.get('/calibration-in-progress', response_class=JSONResponse)
async def calibration_in_progress(request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await ac_api.calibration_in_progress()


@new_server_integration_router.get('/calibration-in-progress-time', response_class=JSONResponse)
async def calibration_in_progress_time(request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await ac_api.calibration_in_progress_time()
